package lia.searching;

import static lia.util.Utilities.buildIndexDir;

import java.io.File;
import java.io.IOException;

import org.apache.log4j.Logger;
import org.apache.lucene.analysis.SimpleAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Explanation;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

import static lia.util.Constants.*;

/**
 * explainer
 * 
 * @author F.H Dragon(fhdargon@gmail.com)
 * @version 1.0 at 2011/08/13
 */
public class Explainer {
	
	//local variables
	/**
	 * log
	 */
	private static final Logger logger = Logger.getLogger("commonLogger");

	/**
	 * test entry...
	 * 
	 * @param args
	 * @throws IOException 
	 * @throws ParseException 
	 */
	public static void main(String[] args) throws IOException, ParseException {
		logger.debug("Begin:Explainer...");
		//build dir
		String indexDir = buildIndexDir("n");
		Directory dir = FSDirectory.open(new File(indexDir));
		logger.debug(dir);
		//build parser
		QueryParser parser = new QueryParser(LUCENE_VERSION, "contents", new SimpleAnalyzer(LUCENE_VERSION));
		//build query
		String q = "junit";
		Query query = parser.parse(q);
		logger.debug("query:" + query);
		
		//build searcher
		IndexSearcher is = new IndexSearcher(dir);
		//search
		TopDocs docs = is.search(query, 20);
		for(ScoreDoc sdoc : docs.scoreDocs){
			Explanation explanation = is.explain(query, sdoc.doc);
			Document doc = is.doc(sdoc.doc);

			logger.debug(doc);
			logger.debug(explanation);
		}
		
		logger.debug("End:Explainer!");
	}

}
